<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function guard()
    {
        return Auth::guard('api');
    }

    public function getUser()
    {
        return $this->guard()->user();
    }

    public function createByMe($model, $field = 'created_user_id')
    {
        $user = $this->getUser();
        return $user->id === $model[$field];
    }

    public function hasPermission($permission, $admin = true)
    {
        $user = $this->getUser();

        if ($user) {
            $permission = collect($permission);

            if ($admin) {
                $permission->push('all');
            }

            return $user->hasAnyPermission($permission->toArray());
        }

        return false;
    }

    protected function responseSuccess($data = null, $code = 200)
    {
        $data = !$data ? null : [
            'code' => 1,
            'data' => $data,
        ];
        return response($data, $code);
    }

    protected function responseError($msg = 'error', $code = 400)
    {
        $data = [
            'code'    => 0,
            'message' => $msg
        ];
        return response($data, $code);
    }

    protected function withToken($token)
    {
        return [
            'access_token' => $token,
            'token_type'   => 'bearer',
            'expires_in'   => $this->guard()->factory()->getTTL() * 60
        ];
    }

    protected function respondWithToken($token)
    {
        return $this->responseSuccess($this->withToken($token));
    }

    protected function respondPermission()
    {
        return response([
            'code'    => 4,
            'message' => '老弟,没有权限'
        ], 403);
    }

    protected function modelFillSave($model, $data)
    {
        $model->fill($data);
        $model->save();
        return $this->responseSuccess($model);
    }


    protected function syncModelPermission($model)
    {
        $permission = request()->get('permissions', false);

        if (is_array($permission)) {
            $permission = arrInPermission(Permission::query(), $permission);
            $model->syncPermissions($permission->pluck('name'));
        }
    }

    protected function syncModelRole($model)
    {
        $role = request()->get('role', false);
        if ($role) {

//            $role = arrInPermission(Role::query(), $role);
            $model->syncRoles($role);
        }
    }

    protected function orderBy($query, $field = 'id', $dis = 'desc')
    {
        $request = request();
        $field   = $request->get('order_field', $field);
        $dis     = $request->get('order_dis', $dis);

        $query->orderBy($field, $dis);
    }

}
